package com.mialab.ManyToMany_demo.main;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.mialab.ManyToMany_demo.domain.Employee;
import com.mialab.ManyToMany_demo.domain.Job;
import com.mialab.ManyToMany_demo.mapper.EmployeeMapper;
import com.mialab.ManyToMany_demo.mapper.JobMapper;
import com.mialab.ManyToMany_demo.utils.DBOperatorMgr;

public class ManyToMany_Main {

	public static void main(String[] args) {
		System.out.println("Hello");
		testSelectEmployeeById("HW9806");
		// testSelectJobById("2601");
	}

	private static void testSelectJobById(String job_id) {
		Logger log = Logger.getLogger(ManyToMany_Main.class);
		SqlSession session = null;
		try {
			session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			Job job = mapper.selectJobById(job_id);
			// System.out.println(job);
			log.info(job);
			// log.info(job.getEmployees());
			List<Employee> employees = job.getEmployees();
			for (Employee employee : employees) {
				System.out.println(employee);
				// log.info(employee);
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	private static void testSelectEmployeeById(String emp_no) {
		Logger log = Logger.getLogger(ManyToMany_Main.class);
		SqlSession session = null;
		try {
			session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();
			EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
			Employee employee = mapper.selectEmployeeById(emp_no);
			// System.out.println(employee);
			log.info(employee);
			// log.info(employee.getJobs());
			List<Job> jobs = employee.getJobs();
			for (Job job : jobs) {
				System.out.println(job);
				// log.info(job);
			}

		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
